package com.cn.springcloudshop.service.impl;

import com.cn.springcloudshop.mapper.YmShopReceivingAddressMapper;
import com.cn.springcloudshop.model.YmShopCar;
import com.cn.springcloudshop.model.YmShopReceivingAddress;
import com.cn.springcloudshop.model.YmShopReceivingAddressExample;
import com.cn.springcloudshop.service.inter.ReceivingAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

/**
 * @Author: wuyanzu
 * @Date: 2018/8/21
 */
@Service
public class ReceivingAddressServiceImpl implements ReceivingAddressService {

    @Autowired
    YmShopReceivingAddressMapper ymShopReceivingAddressMapper;

    @Override
    public int countByExample(YmShopReceivingAddressExample example) {
        Assert.notNull(example,"地址不能为空");
        return ymShopReceivingAddressMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(YmShopReceivingAddressExample example) {
        Assert.notNull(example,"地址不能为空");
        Integer res = ymShopReceivingAddressMapper.deleteByExample(example);
        return res == null ? 0 : res;
    }

    @Override
    public int deleteByPrimaryKey(Long receivingId) {
        Assert.notNull(receivingId,"地址不能为空");
        Integer res = ymShopReceivingAddressMapper.deleteByPrimaryKey(receivingId);
        return res == null ? 0 : res;
    }

    @Override
    public int insert(YmShopReceivingAddress record) {
        Assert.notNull(record,"地址不能为空");
        record.setSate("Y");
        record.setCreateTime(new Date());
        List<YmShopReceivingAddress> list = getYmShopReceivingAddresses(record.getUserId());
        if (list == null || list.size() == 0) {
            record.setIsDefault("Y");
        }else {
            record.setIsDefault("N");
        }
        if ("Y".equals(record.getIsDefault()) && list != null) {
            for (YmShopReceivingAddress ymShopReceivingAddress : list) {
                ymShopReceivingAddress.setIsDefault("N");
                ymShopReceivingAddressMapper.updateByPrimaryKey(ymShopReceivingAddress);
            }
        }
        Integer res = ymShopReceivingAddressMapper.insert(record);
        return res == null ? 0 : res;
    }

    @Override
    public int insertSelective(YmShopReceivingAddress record) {
        Assert.notNull(record,"地址不能为空");
        Integer res = ymShopReceivingAddressMapper.insertSelective(record);
        return res == null ? 0 : res;
    }

    @Override
    public List<YmShopReceivingAddress> selectByExample(YmShopReceivingAddressExample example) {
        Assert.notNull(example,"地址不能为空");
        return ymShopReceivingAddressMapper.selectByExample(example);
    }

    @Override
    public YmShopReceivingAddress selectByPrimaryKey(Long receivingId) {
        Assert.notNull(receivingId,"地址不能为空");
        return ymShopReceivingAddressMapper.selectByPrimaryKey(receivingId);
    }

    @Override
    public int updateByPrimaryKeySelective(YmShopReceivingAddress record) {
        Assert.notNull(record,"地址不能为空");
        Integer res = ymShopReceivingAddressMapper.updateByPrimaryKeySelective(record);
        return res == null ? 0 : res;
    }

    @Override
    public int updateByPrimaryKey(YmShopReceivingAddress record) {
        Assert.notNull(record,"地址不能为空");
        record.setUpdateTime(new Date());
        if ("Y".equals(record.getIsDefault())) {
            List<YmShopReceivingAddress> list = getYmShopReceivingAddresses(record.getUserId());
            if (list != null && list.size() > 0) {
                for (YmShopReceivingAddress ymShopReceivingAddress : list) {
                    ymShopReceivingAddress.setIsDefault("N");
                    ymShopReceivingAddressMapper.updateByPrimaryKey(ymShopReceivingAddress);
                }
            }
        }
        Integer res = ymShopReceivingAddressMapper.updateByPrimaryKey(record);
        return res == null ? 0 : res;
    }

    public List<YmShopReceivingAddress> getYmShopReceivingAddresses(Long userId) {
        YmShopReceivingAddressExample ymShopReceivingAddressExample = new YmShopReceivingAddressExample();
        ymShopReceivingAddressExample.createCriteria().andIsDefaultEqualTo("Y").andUserIdEqualTo(userId);
        return ymShopReceivingAddressMapper.selectByExample(ymShopReceivingAddressExample);
    }

}
